package com.kexun.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kexun.common.utils.PageResult;
import com.kexun.common.utils.Result;
import com.kexun.entity.DO.Role;
import com.kexun.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("list")
    @RequiresPermissions(value = "system/role/list")
    public String roleList() {
        return "role/role_list";
    }

    @ResponseBody
    @PostMapping("list")
    @RequiresPermissions(value = "system/role/list")
    public PageResult list(String roleName, int page, int limit) {
        IPage<Role> roleIPage = roleService.roleList(roleName, page, limit);
        return new PageResult(roleIPage.getRecords(), roleIPage.getTotal());
    }

    @GetMapping("edit")
    @RequiresPermissions(value = "system/role/edit")
    public String edit(int id, Model model) {
        Role role = roleService.findRoleById(id);
        model.addAttribute("role", role);
        return "role/role_edit";
    }

    @ResponseBody
    @PostMapping("editRole")
    @RequiresPermissions(value = "system/role/edit")
    public Result editRole(int roleId, String roleName, String ids) {
        //封装到service里面除了,否则可能会出现事务问题
        roleService.updateRole(ids, roleId, roleName);
        return Result.success();
    }

    @GetMapping("add")
    public String edit() {
        return "role/role_add";
    }


    @ResponseBody
    @PostMapping("addRole")
    public Result addRole(String roleName, String ids) {
        //封装到service里面除了,否则可能会出现事务问题
        roleService.addRole(roleName, ids);
        return Result.success();
    }

    //删除
    @ResponseBody
    @PostMapping("del")
    public Result delRole(int id) {
        roleService.delRole(id);
        return Result.success();
    }


}
